﻿<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="search-keywords" content="client, subscription, development" />
<meta name="generator" content="Adobe RoboHelp 9" />
<title>Step 3 - Setup Subscription</title>
<link rel="StyleSheet" href="default.css" type="text/css" />
<style type="text/css">
/*<![CDATA[*/
<!--
P { margin-left:0pt;
margin-right:0pt;
font-family:Verdana;
margin-bottom:3pt;
margin-top:18pt; }
H1 { font-weight:bold;
margin-top:14pt;
margin-bottom:14pt;
margin-left:0pt;
margin-right:0pt;
page-break-after:avoid;
font-family:Verdana;
x-next-type:P;
font-size:18pt; }
H2 { font-weight:bold;
margin-top:14pt;
margin-bottom:14pt;
margin-left:0pt;
margin-right:0pt;
page-break-after:avoid;
font-family:Verdana;
x-next-type:P;
font-size:14pt; }
H3 { font-weight:bold;
margin-top:14pt;
margin-bottom:14pt;
margin-left:0pt;
margin-right:0pt;
page-break-after:avoid;
font-family:Verdana;
x-next-type:P;
font-size:12pt; }
-->
/*]]>*/
</style>

<script type="text/javascript" language="JavaScript">
//<![CDATA[
function reDo() {
  if (innerWidth != origWidth || innerHeight != origHeight)
     location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
        origWidth = innerWidth;
        origHeight = innerHeight;
        onresize = reDo;
}
onerror = null; 
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
<!--
div.WebHelpPopupMenu { position:absolute;
left:0px;
top:0px;
z-index:4;
visibility:hidden; }
-->
/*]]>*/
</style>

<script type="text/javascript" language="javascript1.2" src="whmsg.js">
</script>
<script type="text/javascript" language="javascript" src="whver.js">
</script>
<script type="text/javascript" language="javascript1.2" src="whproxy.js">
</script>
<script type="text/javascript" language="javascript1.2" src="whutils.js">
</script>
<script type="text/javascript" language="javascript1.2" src="whlang.js">
</script>
<script type="text/javascript" language="javascript1.2" src="whtopic.js">
</script>
</head>
<body>
<script type="text/javascript" language="javascript1.2">
//<![CDATA[
<!--
if (window.gbWhTopic)
{
        var strUrl = document.location.href;
        var bc = 0;
        var n = strUrl.toLowerCase().indexOf("bc-");
        if(n != -1)
        {
                document.location.href = strUrl.substring(0, n);
                bc = strUrl.substring(n+3);
        }

        if (window.addTocInfo)
        {
        addTocInfo("Development\nClient\nStep 3 - Setup Subscription");
addButton("show",BTN_TEXT,"Show","","","","",0,0,"","","");

        }
        if (window.writeBtnStyle)
                writeBtnStyle();

        if (window.writeIntopicBar)
                writeIntopicBar(1);

        
        document.write("<p style=\"font-family: Arial; font-size: 8pt; font-weight: 400;  font-style:normal; color: rgb(0, 0, 255); text-decoration:none; text-align: right\"> ");
AddMasterBreadcrumbs("index.htm", "font-family: Arial; font-size: 8pt; font-weight: 400;  font-style:normal; color: rgb(0, 0, 255); text-decoration:none; text-align: right", "&gt;", "Home", "welcome.htm");
document.write("<a style=\"font-family: Arial; font-size: 8pt; font-weight: 400;  font-style:normal; color: rgb(0, 0, 255); text-decoration:none; text-align: right\" href=\"developing_ua_applications.htm\">Development<\/a> &gt; <a style=\"font-family: Arial; font-size: 8pt; font-weight: 400;  font-style:normal; color: rgb(0, 0, 255); text-decoration:none; text-align: right\" href=\"client_development.htm\">Client<\/a> &gt; Step 3 - Setup Subscription<\/p>");


        if (window.setRelStartPage)
        {
        setRelStartPage("index.htm");

                autoSync(1);
                sendSyncInfo();
                sendAveInfoOut();
        }
}
else
        if (window.gbIE4)
                document.location.reload();

//-->
//]]>
</script><span style="font-weight: bold; font-size: 10pt;"><font size="2" style="font-size:10pt;"><b><a href="http://www.opcfoundation.org" style="color: #0000ff; font-size: 10pt; text-decoration: underline;"><img src="opcfoundation.jpg" alt="opcfoundation.jpg" style="border: none;" width="189" height="61" align="left" border="0" /></a>The Interoperability Standard<br />
for Industrial Automation</b></font></span><br />
&#160;<br />
&#160;
<table cellspacing="0" width="100%" align="center">
<tr>
<td style="vertical-align: top;"><span style="font-size: 8pt;"><font size="1" style="font-size:8pt;"><a href="http://www.opcfoundation.org" target="_blank" style="color: #0000ff; text-decoration: underline;">OPC Foundation Online</a> | <a href="http://www.opcfoundation.org/certification" target="_blank">Certification</a> | <a href="http://www.opcfoundation.org/technology" target="_blank">Technology</a> | <a href="https://www.opcfoundation.org/news" target="_blank">News</a></font></span></td>
<td style="vertical-align: top; text-align: right; font-size: 8pt;"><a href="welcome.htm" style="color: #0000ff; text-decoration: underline;">Home</a> | <a href="getting_started.htm">Getting Started</a></td>
</tr>
</table>
<p style=" border-bottom:Solid 1px #000000; font-weight:bold; text-align:center; border-width:4px;"><span style="font-weight: bold; text-align: center;"><b><span style="font-weight: bold;"><b>Unified Architecture Technology Sample Applications</b></span></b></span></p>
<h1>Client Development Tutorial</h1>
<h2>Step 3 - Setup a Subscription</h2>
<p>In this step we will do the following:</p>
<ul type="disc">
<li>
<p>Attach an event handler to the new "Subscribe" menu item which will do the following:</p>
</li>
<li style="list-style: none; display: inline">
<ul type="disc">
<li>
<p>Create a new subscription.</p>
</li>
<li>
<p>Add 1 monitored item that will poll every second.</p>
</li>
</ul>
</li>
</ul>
<h3>Attaching the Event Handler</h3>
<ol type="1">
<li>
<p>Highlight the "Subscription" menu item, and then double-click it; this will create a new/empty event handler.</p>
</li>
<li>
<p>The cursor will be automatically positioned to the first empty line. This is where you will start coding (next).</p>
</li>
</ol>
<h3>Creating the New Subscription</h3>
<ol type="1">
<li>
<p>First, we need to define our subscription object. Our use of this subscription in this tutorial will be very basic, meaning that we will simply create the subscription and assume that it will remain alive throughout the lifetime of the connection.</p>
</li>
<li>
<p>Move the cursor above the event handler and copy/paste the following code:<br />
<span style="font-family: 'Courier New', monospace;">private Subscription m_subscription;</span></p>
</li>
<li>
<p>Move the cursor back into the event handler and now define our subscription, but only if it has not already been defined:<br />
<span style="font-family: 'Courier New', monospace;">if( m_subscription == null )<br />
{<br />
&#160;m_subscription = new Subscription(m_session.DefaultSubscription);<br />
&#160;m_subscription.PublishingEnabled=true;<br />
&#160;m_subscription.PublishingInterval=1000;<br />
&#160;m_session.AddSubscription(m_subscription);<br />
&#160;m_subscription.Create();<br />
}</span></p>
</li>
</ol>
<h3>Adding the Monitored Item</h3>
<ol type="1">
<li>
<p>Now we will take the NodeId of the currently selected node, and will turn it into a monitored item which we will add to our subscription.</p>
</li>
<li>
<p>Move the cursor above the event handler and create a monitored item variable:<br />
<span style="font-family: 'Courier New', monospace;">private MonitoredItem monitoredItem;</span></p>
</li>
<li>
<p>Move the cursor back into the event handler and create the following "if block" below the code that you have previously created:<br />
<span style="font-family: 'Courier New', monospace;">if( monitoredItem == null )<br />
{<br />
&#160;monitoredItem = new MonitoredItem( m_subscription.DefaultItem );<br />
&#160;&#160;monitoredItem.StartNodeId =<br />
&#160;&#160;monitoredItem.AttributeId = Attributes.Value;<br />
&#160;&#160;monitoredItem.MonitoringMode = MonitoringMode.Reporting;<br />
&#160;&#160;monitoredItem.SamplingInterval = 1000;<br />
&#160;&#160;monitoredItem.QueueSize = 0;<br />
&#160;&#160;monitoredItem.DiscardOldest = true;<br />
&#160;// define event handler for this item, and then add to subscription<br />
&#160;&#160;monitoredItem.Notification += new MonitoredItemNotificationEventHandler( monitoredItem_Notification);<br />
&#160;m_subscription.AddItem( monitoredItem );<br />
}</span></p>
</li>
</ol>
<p>Next, <a href="step_4_-_consuming_datachanges.htm">Step 4 - Consuming Data-changes</a>.</p>
<p style=" border-top:Solid 1px #000000; font-size:6pt; text-align:right; border-width:4px;"><span style="font-size: 6pt; text-align: right;"><font size="1" style="font-size:6pt;">Copyright OPC Foundation 1995-2013</font></span></p>
<script type="text/javascript" language="javascript1.2">
//<![CDATA[
<!--
if (window.writeIntopicBar)
        writeIntopicBar(0);


highlightSearch();
//-->
//]]>
</script>
</body>
</html>
